import {searchPage,addEntity,searchOrder,searchOrderDetail,searchOne,updateEntity,deleteApplyByApplyIdList} from '../../../api/b2cReturns'
import {searchGoodsVo} from '../../../api/product'
let apply = {
    data() {
        return {
            //日期组件
            createTimeOptions:'',
            approvalTimeOptions:'',
            //table的list
            tableData: [],
            total:0,
            searchParams:{
                currentPage :1,
                pageSize:5
            },
            approvalStatusOptions:[
                {
                    value:null,
                    label:"全部"
                },
                {
                    value: 0,
                    label: '未审批'
                }, {
                    value: 1,
                    label: '已通过'
                }, {
                    value:2,
                    label: '未通过'
                }
            ],
            goodStatusOptions:[
                {
                    value: 0,
                    label: '退货'
                }, {
                    value: 1,
                    label: '同机型换货'
                }, {
                    value:2,
                    label: '不同机型换货'
                }
            ],
            dialogVisible:false,
            formData:{},
            editFormVisible:false,
            editApplyForm:{
                applyId:'',
                sellCode:'',
                complaintId:'',
                producer:'admin',
                producDept:'售后部门',
                remarks:'',
                status:0,
                overhaulId:'',
                overhaulName:'',
                overhaulTime:'',
                overhaulPhone:'',
                approver:'',
                approvalTime:'',
                approvalIdea:'',
                approvalStatus:0,
                createTime:null,
                entryId:'',
                list:[],
                listNew:[]
            },
            orderData:{
                orderCode:'',
                orderType:'',
                orderAction:'',
                businessType:'',
                orderState:'',
                orderCreateTime:'',
                consigneeName:'',
                memberAccount:'',
                phoneNumber:'',
                cellphoneNumber:'' ,
                postalCode:'',
                deliveryAddress:'',
                deliveryArea:'',
                deliveryWay:'' ,
                deliveryCost:'',
                payWay:'',
                selfPickupTime:'',
                email:'',
                payer:'',
                remark:'',
                sellOrderGoodsList:[],
            },
            editForm:true,
            title:'',
            chooseGoodVisible:false,
            orderGoodDate:[],
            chooseGoodDate:[],
            chooseOrderVisible:false,
            chooseOrderDate:[],
            orderFindDate:[],
            orderTotal:0,
            searchOrderParams:{
                currentPage :1,
                pageSize:5
            },
            deleteIds:[],
            chooseNewGoodVisible:false,
            goodTotal:0,
            newGoodFindDate:[],
            searchGoodParams:{
                currentPage :1,
                pageSize:5
            },
            newGood:{
                newId:"",
                connectCode:'',
                goodName:'',
                goodCode:''
            }
        };
    },
    methods:{
        //添加申请单
        addVisible(){
            this.editFormVisible = true;
            this.editForm = false;
            this.title="添加申请单";
            this.findOrder();
        },
        //查找订单
        async findOrder(){
            let response =  await searchOrder(this.searchOrderParams);
            this.orderTotal= response.total;
            this.orderFindDate = response.records;
        },
        //订单换页
        currentOrderPageChange(currentPage){
            this.searchOrderParams.currentPage = currentPage;
            this.findOrder();
        },
        //选择订单
         chooseOrder(){
            this.chooseOrderVisible = true;
        },
        //添加订单
        addOrder(order){
            this.orderDetail(order.sellCode);
            this.editApplyForm.sellCode = order.sellCode;
            this.chooseOrderVisible = false;
            this.editApplyForm.list = [];
        },
        //订单展示
        async orderDetail(sellCode){
            let response =  await searchOrderDetail(sellCode);
            this.orderData = JSON.parse(JSON.stringify(response));
            this.orderData.selfPickupTime = this.changeDate(this.orderData.selfPickupTime);
            this.orderData.orderCreateTime = this.changeDate(this.orderData.orderCreateTime);
        },
        //选择退换货商品
        async chooseGood(){
            this.chooseGoodVisible = true;
            let good= this;
            await this.orderData.sellOrderGoodsList.forEach(function (value, index, array) {
                let num ={key:'',
                    label:''};
                num.key = value.connectCode;
                num.label = value.goodName;
                good.orderGoodDate.push(num);
            });
            if(this.editApplyForm.list !== null){
                this.editApplyForm.list.forEach(function (value) {
                    let num = good.orderGoodDate;
                    good.chooseGoodDate.push(value.connectCode);
                    for(let i = 0;i<num.length;i++){
                        if(value.connectCode === num[i]){
                            num.delete(i);
                        }
                    }
                })
            }
        },
        //退换货商品菜单关闭
        resetChooseGood(){
            this.chooseGoodVisible = false;
            this.orderGoodDate=[];
            this.chooseGoodDate=[];
        },
        //添加退换货商品
        addChooseGood(){
            let good= this;
            this.editApplyForm.list = [];
            this.editApplyForm.listNew = [];
            this.orderData.sellOrderGoodsList.forEach(function (value, index, array) {
                let num = good.chooseGoodDate;
                for(let i = 0;i<num.length;i++){
                    if(value.connectCode === num[i]){
                        good.editApplyForm.list.push(value);
                    }
                }
            });
            this.resetChooseGood();
        },
        //打开新商品界面
        openNewGood(row){
            this.findNewGood();
            this.newGood.newId = row.connectCode;
            this.chooseNewGoodVisible = true;
        },
        //新商品换页
        currentGoodPageChange(page){
            this.searchGoodParams.currentPage = page;
            this.findNewGood();
        },
        //查找新商品
        async findNewGood(){
            let response =  await searchGoodsVo(this.searchGoodParams);
            this.goodTotal= response.total;
            this.newGoodFindDate = response.data;
        },
        //选择新商品
        addNewGood(row){
            this.newGood.connectCode = row.goodCode;
            this.newGood.goodCode = row.goodId;
            this.newGood.goodName = row.goodName;
            let good = JSON.parse(JSON.stringify(this.newGood))
            this.editApplyForm.listNew.push(good);
        },
        //删除新商品
        deleteNewGood(row,index){
            this.editApplyForm.listNew.splice(index,1);
        },
        //关闭新商品选择界面
        resetChooseNewGood(){
            this.newGood.newId = '';
            this.chooseNewGoodVisible = false;
        },
        //新增申请单
        async addApply(){
            let response =  await addEntity(this.editApplyForm);
            this.resetFields();
        },
        //修改初始化
        async updateVisible(apply){
            let response =await searchOne(apply.applyId);
            this.editFormVisible = true;
            this.editForm = false;
            this.title="修改申请单";
            this.findOrder();
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        //修改申请单
        async updateApply(){
            this.editApplyForm.approver='';
            this.editApplyForm.approvalTime='';
            this.editApplyForm.approvalIdea='';
            this.editApplyForm.approvalStatus=0;
            let response =  await updateEntity(this.editApplyForm);
            this.resetFields();
        },
        //查看
        async applyDetail(apply){
            let response =await searchOne(apply.applyId);
            this.editFormVisible = true;
            this.title="查看申请单";
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            this.editApplyForm.approvalTime = this.changeDate(this.editApplyForm.approvalTime);
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        //审核跟踪
        async approvalDetail(apply){
            this.editFormVisible = true;
            this.title="审核跟踪";
            let response =await searchOne(apply.applyId);
            this.editApplyForm=JSON.parse(JSON.stringify(response));
            this.editApplyForm.approvalTime = this.changeDate(this.editApplyForm.approvalTime);
            if(response.sellCode!==null){
                this.orderDetail(response.sellCode);
            }
        },
        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange(selection){
            this.deleteIds = [];
            let ids=this;
            selection.forEach(function (value, index) {
                ids.deleteIds.push(value.applyId);
            });
        },
        //批量删除
        async deleteByIds(){
            await deleteApplyByApplyIdList(this.deleteIds);
            this.findPage();
        },
        //换页
        currentPageChange(currentPage){
            this.searchParams.currentPage = currentPage;
            //console.count(this.searchParams.currentPage);
            this.findPage();
        },
        //表格日期转换
        formatDate(row, column){
            let date = row[column.property];
            return this.changeDate(date);
        },
        //日期转换
        changeDate(date){
            if(date === null){
                return date;
            }
            let one = date.indexOf('-');
            let year = date.slice(0,one);
            let month = date.slice(one+1,one+3);
            let day = date.slice(one+4,one+6);
            let time = date.slice(date.indexOf('T')+1);
            return year+'年'+month+'月'+day+'日'+' '+time;
        },
        //查找
       async findPage(){
           let response =  await searchPage(this.searchParams);
               this.total= response.total;
               this.tableData = response.data;
        },
        //创建时间选择
        chooseCreateTime(){
            this.searchParams.startTime=this.createTimeOptions[0];
            this.searchParams.endTime=this.createTimeOptions[1];
        },
        //审批时间选择
        chooseApprovalTime(){
            this.searchParams.approvalStartTime=this.approvalTimeOptions[0];
            this.searchParams.approvalEndTime=this.approvalTimeOptions[1];
        },
        //重置查找表单
        resetForm(){
            this.searchParams={  currentPage :1, pageSize:5};
            this.createTimeOptions ='';
            this.approvalTimeOptions ='';
            this.findPage();
        },
        //重置添加表单
        resetFields(){
            this.editFormVisible = false;
            this.editForm = true;
            this.orderData={};
            this.editApplyForm={
                applyId:'',
                    orderId:'',
                    complaintId:'',
                    producer:'admin',
                    producDept:'售后部门',
                    remarks:'',
                    status:0,
                    overhaulId:'',
                    overhaulName:'',
                    overhaulTime:'',
                    overhaulPhone:'',
                    approver:'',
                    approvalTime:'',
                    approvalIdea:'',
                    approvalStatus:0,
                    createTime:'',
                    entryId:'',
                    list:[],
                    listNew:[]
            };
            this.findPage();
        }
    },
    created(){
        this.findPage();
    }
};

export default apply;